/*
 * Copyright 2002-2006 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.jwtools.tmt.ui;

import java.util.Date;
import java.util.List;

import javax.swing.JComponent;

import net.jwtools.tmt.domain.IVisit;
import net.jwtools.tmt.domain.Person;
import net.jwtools.tmt.service.PersonService;

import org.springframework.binding.form.FieldMetadata;
import org.springframework.richclient.form.AbstractForm;
import org.springframework.richclient.form.binding.Binding;
import org.springframework.richclient.form.binding.swing.SwingBindingFactory;
import org.springframework.richclient.form.builder.TableFormBuilder;


/**
 * Form to handle the properties of a Contact object. It uses a
 * {@link TableFormBuilder} to construct the layout of the form. Contact object
 * properties are easily bound to UI controls using the form builder's
 * {@link TableFormBuilder#add(String)} method. The platform takes care of
 * determining which kind of control to create based on the type of the property
 * in question.
 * 
 * @author Larry Streepy
 */
public class TerritoryCheckoutForm extends AbstractForm {

	public TerritoryCheckoutForm(IVisit visit) {
		super(visit);
		visit.setCheckoutDate(new Date());
		setId("visit");
	}

	protected JComponent createFormControl() {
		SwingBindingFactory bindingFactory = (SwingBindingFactory) getBindingFactory();
		TableFormBuilder formBuilder = new TableFormBuilder(getBindingFactory());
		formBuilder.setLabelAttributes("colGrId=label colSpec=right:pref");
		PersonService personService = (PersonService) getApplicationServices()
				.getService(PersonService.class);
		formBuilder.addSeparator("Checkout");
		formBuilder.row();
		FieldMetadata md = getFormModel().getFieldMetadata( "territory.number" );
		md.setReadOnly( true );
		formBuilder.add("territory.number", "colSpan=1 align=left");
		formBuilder.row();
		List<Person> persons = personService.getAllPersons();
		Binding displayName = bindingFactory.createBoundComboBox("checkoutBy",
				persons, "displayName");
		formBuilder.add(displayName);
		formBuilder.row();
		formBuilder.add("checkoutDate", "colSpan=1 align=left");
		
		return formBuilder.getForm();
	}

	/**
	 * Try to place the focus in the firstNameField whenever the initial focus
	 * is being set.
	 * 
	 * @return
	 */
	public boolean requestFocusInWindow() {
		return this.requestFocusInWindow();
	}
}